/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 20:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using Orz.LogManager.Config;
using Orz.Xml;
using System.Xml.Schema;
using System.Xml;
using System.Xml.Serialization;


namespace Orz.LogManager
{
/// <summary>
/// Description of LogsetLoader.
/// </summary>
public class LogsetLoader
{
	private static String sSchemaFileLoc_c = Application.StartupPath + "\\logs.xsd";

	private static bool bSchemaFileExists_c = CheckForSchemaFile();
	
	public LogsetLoader()
	{		
		
	}
	
	
	
	public static bool isSSH (logsetLogfile file_p)
	{
		return protocolType.ssh == file_p.telnetProtocol;		
	}
	
	public static bool isTelnet (logsetLogfile file_p)
	{
		return (null != file_p.telnetHost  &&  file_p.telnetHost.Length > 0  &&  ! isSSH(file_p));
	}
	
	
	
	public static bool CheckForSchemaFile ()
	{
		bool bExists = System.IO.File.Exists (sSchemaFileLoc_c);
		
		if (!bExists)
		{
			MessageBox.Show("Warning: couldn't find the logs.xsd schema file in " + sSchemaFileLoc_c, "Logset Loader", MessageBoxButtons.OK, MessageBoxIcon.Warning);
		}
		
		return bExists;
	}
	
	
	
	
	
	
	public static LogsetWithFile LoadLogsetFromFile(String sConfigFileName_p)
	{
		
		logset l;
		
		//================================================================
		// Get the XML schema from the local directory if possible
		//================================================================				
		if (bSchemaFileExists_c)
		{
			System.IO.Stream schemaStream = File.OpenRead(sSchemaFileLoc_c);			
			System.Xml.XmlReader schemaReader = new System.Xml.XmlTextReader(schemaStream);
			
			l = (logset) XmlUtils.DeserialiseFileWithValidation(typeof(logset), sConfigFileName_p, schemaReader);
		}
		else
		{
			l = (logset) XmlUtils.DeserialiseFile(typeof(logset), sConfigFileName_p);
		}	
		
		String[] vParts = sConfigFileName_p.Split('\\');
		String sFilename = vParts[vParts.Length-1];
		
		
		LogsetWithFile lwf = new LogsetWithFile(l);
		lwf.FileName = sFilename;
		
		return lwf;

	}
	
	
	
	public static LogsetWithFile CloneLogsetWithFile (LogsetWithFile src_p)
	{
		LogsetWithFile dest = new LogsetWithFile();
		dest.FileName = src_p.FileName;
		
		dest.Logset = CloneLogset (src_p.Logset);
		
		return dest;
	}
	
	public static logset CloneLogset (logset src_p)
	{
		XmlSerializer ser = new XmlSerializer(typeof(logset));
		
		// Serialise to Xml string
		StringWriter w = new StringWriter();	
		ser.Serialize(w, src_p);
		String sXml = w.ToString();
				
		// Deserialise to an object		
		StringReader r = new StringReader(sXml);							
		logset dest = (logset) ser.Deserialize(r);
		
		if (null == dest.logfile)
		{
			dest.logfile = new logsetLogfile[0];
		}
		
		return dest;
	}
	
	
	
	
	public static logsetLogfile CloneLogfile (logsetLogfile src_p)
	{
		XmlSerializer ser = new XmlSerializer(typeof(logsetLogfile));
		
		// Serialise to Xml string
		StringWriter w = new StringWriter();	
		ser.Serialize(w, src_p);
		String sXml = w.ToString();
				
		// Deserialise to an object		
		StringReader r = new StringReader(sXml);							
		logsetLogfile dest = (logsetLogfile) ser.Deserialize(r);
		
		if (null == dest.autoLogin)
		{
			dest.autoLogin = new logsetLogfileAutoLogin[0];
		}
		
		if (null == dest.match)
		{
			dest.match = new logsetLogfileMatch[0];
		}
		
		
		return dest;
	}
	
	
	
	public static logsetLogfileMatch CloneLogfileMatch (logsetLogfileMatch src_p)
	{
		logsetLogfileMatch dest = new logsetLogfileMatch();
		dest.pattern = src_p.pattern;
		dest.colour = src_p.colour;
		dest.replacement = src_p.replacement;
		dest.dontPrint = src_p.dontPrint;
		
		return dest;
	}
	
	
	
	
	
	
	
	
	public static void SaveLogsetWithFile (LogsetWithFile set_p)
	{
		String sFileName = Application.StartupPath + "\\" + set_p.FileName;
		XmlUtils.SerialiseObject (set_p.Logset, sFileName);		
	}
	
	
	
	public static void DeleteLogset (String sFileName_p)
	{
		String sFileName = Application.StartupPath + "\\" + sFileName_p;
		File.Delete (sFileName);
	}

	
	
	public static IList GetLogsets()
	{
		String sStartDir = Application.StartupPath;
		
		String[] vFileNames = Directory.GetFiles(sStartDir, "*.xml");
		
		IList logsets = new ArrayList();				
		
		for (int i=0; i<vFileNames.Length; i++)
		{
			String sFilePath = vFileNames[i];
			try
			{
				LogsetWithFile l = LogsetLoader.LoadLogsetFromFile(sFilePath);
				//MessageBox.Show ("Adding " + l.name + " from " + sFilePath);
				logsets.Add (l);
				
			}
			catch (Exception e)
			{
				if (! sFilePath.EndsWith("Log Manager.xml"))
				{	
					throw new Exception("Error opening configuration file <" + sFilePath + ">\n", e);
				}
			}
		}
			
		return logsets;		
	}
	
	
}
}
